<?php 
include "config.php";
$tos = $_POST['to'];
//echo $tos."<br>";
$to = explode (',',$tos);
$from = $_POST['from'];
//$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$from.'+Indonesia&sensor=true');
//$geocode=file_get_contents(json);
//$output= json_decode($geocode);
//$latitude1 = $output->results[0]->geometry->location->lat;
//$longitude1 = $output->results[0]->geometry->location->lng;
//echo $latitude1 ."-". $longitude1."<br>";

$key = "ABQIAAAAlSrsSTN5DgS8ZVvOIMXgaRRko6GAsd-NuNaX8qRhTW-5iKUrrhQAEyRuTDiP8alMZuDudl0V6ctgMw";
$address = urlencode($from);
 
//If you want an extended data set, change the output to "xml" instead of csv
$url = "http://maps.google.com/maps/geo?q=".$address."&output=csv&key=".$key;
//Set up a CURL request, telling it not to spit back headers, and to throw out a user agent.
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER,0); //Change this to a 1 to return headers
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
$data = curl_exec($ch);
curl_close($ch);
 

$geo = explode(',', $data);
$latitude1 = $geo[2];
$longitude1 = $geo[3];

//echo $latitude1 ."-". $longitude1."<br>";	

$distancesawal = 100000000000000000000000000;
$result = mysql_query("SELECT id, lat, lng FROM `jalur` where status = 1");

while($r = mysql_fetch_array($result)) 
   { 
	$latitude2 = $r[lat];
	$longitude2 = $r[lng];
	$id = $r[id];
	$theta = $longitude1 - $longitude2;
	$distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) +
	(cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) *
	cos(deg2rad($theta)));
	$distance = acos($distance);
	$distance = rad2deg($distance);
	$distance = $distance * 60 * 1.1515;
	$distances = $distance * 1609.344;
//	echo "id ".$id." jaraknya ".$distances." dimana distance awal ".$distancesawal." maka id fix ".$idfix."<br>";
		if ($distancesawal > $distances){
			$idfix = $id;
			$distancesawal = $distances ;
		}
		
	}
//echo $idfix."<br>";


	
$latitude1 = $to[0];
$longitude1 = $to[1];
//echo $latitude1 ."-". $longitude1."<br>";	
$distancesawal = 100000000000000000000000000;
$result = mysql_query("SELECT id, lat, lng FROM `jalur` where status = 1");
while($r = mysql_fetch_array($result)) 
   { 
	$latitude2 = $r[lat];
	$longitude2 = $r[lng];
	$id = $r[id];
	$theta = $longitude1 - $longitude2;
	$distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) +
	(cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) *
	cos(deg2rad($theta)));
	$distance = acos($distance);
	$distance = rad2deg($distance);
	$distance = $distance * 60 * 1.1515;
	$distances = $distance * 1609.344;
//echo "id ".$id." jaraknya ".$distances." dimana distance awal ".$distancesawal." maka id fix ".$idfixed."<br>";
		if ($distancesawal > $distances){
			$idfixed = $id;
			$distancesawal = $distances ;
		}
		
	}
//echo $idfixed."<br>";

echo "<meta http-equiv='refresh' content='0;url=http://nusantaraview.web.id/PETA/dijkstra.php?a=$idfix&b=$idfixed'>";


?>